﻿using System;
using System.CodeDom;
using Roslyn.Compilers.CSharp;

namespace WeaverCustomTool
{
    public static class TypeDeclarationExtensions
    {
        public static CodeTypeReference ToTypeReference(this TypeSyntax typeDeclaration)
        {
            string typeName = null;
            if(typeDeclaration is IdentifierNameSyntax)
            {
                typeName = ((IdentifierNameSyntax) typeDeclaration).Identifier.ValueText;
            }
            else
            {
                typeName = ((PredefinedTypeSyntax)typeDeclaration).Keyword.ValueText;
            }

            switch (typeName)
            {
                case "string":
                    return new CodeTypeReference(typeof(string));
                case "int":
                    return new CodeTypeReference(typeof(int));
                case "float":
                    return new CodeTypeReference(typeof(float));
                case "char":
                    return new CodeTypeReference(typeof(char));
                case "byte":
                    return new CodeTypeReference(typeof(byte));
                case "sbyte":
                    return new CodeTypeReference(typeof(sbyte));
                case "decimal":
                    return new CodeTypeReference(typeof(decimal));
                case "uint":
                    return new CodeTypeReference(typeof(uint));
                case "long":
                    return new CodeTypeReference(typeof(long));
                case "ulong":
                    return new CodeTypeReference(typeof(ulong));
                case "object":
                    return new CodeTypeReference(typeof(object));
                case "short":
                    return new CodeTypeReference(typeof(short));
                case "ushort":
                    return new CodeTypeReference(typeof(ushort));
            }

            return new CodeTypeReference(typeName);
        }

        /// <summary>
        /// Implements the specified interface.
        /// </summary>
        /// <param name="interface">The interface.</param>
        /// <returns></returns>
        public static CodeTypeDeclaration Implement(this InterfaceDeclarationSyntax @interface)
        {
            return new TypeDeclaration().FromInterface(@interface);
        }
    }
}
